package com.huilan.hgt.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huilan.hgt.entity.User;
import com.huilan.hgt.entity.UserRole;
import com.huilan.hgt.mapper.UserMapper;
import com.huilan.hgt.mapper.UserRoleMapper;
import com.huilan.hgt.service.UserRoleService;
import com.huilan.hgt.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huilan.hgt.util.MD5Util;
import com.huilan.hgt.util.PageUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author ghy
 * @since 2018-11-02
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    UserRoleMapper userRoleMapper;

    /**
     * 根据用户名查询密码
     *
     * @param username
     * @return
     */
    @Override
    public User getUserByName(String username) {
        QueryWrapper<User>  queryWrapper= new QueryWrapper<>();
        queryWrapper.setEntity(new User().setUsername(username));
        User user = userMapper.selectOne(queryWrapper);
        if(user!=null){
            return user;
        }
        return null;

    }

    /**
     * 查询邮箱列表
     *
     * @return
     */
    @Override
    public List<User> queryEmails() {
        QueryWrapper<User>  queryWrapper= new QueryWrapper<>();
        queryWrapper.select(User.EMAIL_ADDRESS);
        queryWrapper.isNotNull(User.EMAIL_ADDRESS);
        return userMapper.selectList(queryWrapper);
    }

    /**
     * 查询用户信息
     *  @param page
     * @param user
     */
    @Override
    public PageUtil<User> queryUserList(PageUtil page, User user) {
        return userMapper.queryUserList(page,user);
    }

    /**
     * 修改或保存用户信息
     *
     * @param user
     * @param roleId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdate(User user, String roleId) {
        boolean flag = false;
        try{
            if(user.getUserId()!=null){
                if(StringUtils.isNotEmpty(user.getPassword())){
                    user.setPassword(MD5Util.encryption(user.getPassword()));
                }
                userMapper.updateById(user);

                //先删除该用户角色信息
                Map<String,Object> params = new HashMap<>();
                params.put(UserRole.USER_ID,user.getUserId());
                userRoleMapper.deleteByMap(params);

                //再添加用户角色信息
                if(StringUtils.isNotEmpty(roleId)){
                    UserRole userRole =  new UserRole();
                    userRole.setRoleId(Integer.parseInt(roleId))
                            .setUserId(user.getUserId());
                    userRoleMapper.insert(userRole);
                }
                flag = true;
            }else{
                userMapper.insert(user);
                UserRole userRole =  new UserRole();
                userRole.setRoleId(Integer.parseInt(roleId))
                        .setUserId(user.getUserId());
                userRoleMapper.insert(userRole);
                flag = true;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }


}
